/**
 * @file ctp_hynitron_iic.c
 * @descripttion: cst8xx iic driver
 * @author: yabo.wang@transtekcorp.com
 * @date: 2020-08-18 18:00:27
 *
 * @copyright Copyright (c) 2020
 *
 */

//#include "FreeRTOS.h"
#include "capacitive_tp_hynitron_cst0xx.h"
#include "ctp_hynitron_iic.h"
#include "gr55xx_sys_sdk.h"
#include "app_io.h"
#include "app_i2c.h"
#include "../images/ui.h"
//#include "TpIicDriver.h"


/*
*************************************************************************
*                              函数定义
*************************************************************************
*/
static app_i2c_params_t tp_params = {
    .id = APP_I2C_ID_3,
    .role = APP_I2C_ROLE_MASTER,
    .pin_cfg = {
        .scl = {
            .type = APP_IO_TYPE_GPIOA,
            .mux  = APP_IO_MUX_0,
            .pin  = APP_IO_PIN_6,
            .pull = APP_IO_PULLUP,
        },
        .sda = {
            .type = APP_IO_TYPE_GPIOA,
            .mux  = APP_IO_MUX_0,
            .pin  = APP_IO_PIN_7,
            .pull = APP_IO_PULLUP,
        },
    },
    .dma_cfg = {
    #if (APP_DRIVER_CHIP_TYPE == APP_DRIVER_GR5332X) || (APP_DRIVER_CHIP_TYPE == APP_DRIVER_GR551X)
        .tx_dma_instance = DMA0,
        .rx_dma_instance = DMA0,
    #else
        .tx_dma_instance = DMA1,
        .rx_dma_instance = DMA1,
    #endif
        .tx_dma_channel  = DMA_Channel0,
        .rx_dma_channel  = DMA_Channel1,
    },
    .init = {
        .speed = I2C_SPEED_400K,
        .own_address = 0x00,
        .addressing_mode = I2C_ADDRESSINGMODE_7BIT,
        .general_call_mode = I2C_GENERALCALL_DISABLE,
    },
};

/**
 * @brief   cst8xx 硬件复位
 *
 */
void hctp_reset_ic(void)
{
   // am_hal_gpio_pinconfig(CTP_RESET_PIN, g_AM_HAL_GPIO_OUTPUT_4);
   // am_hal_gpio_output_clear(CTP_RESET_PIN);
		uint16_t ret;
    app_io_init_t io_init = APP_IO_DEFAULT_CONFIG;	
    io_init.pull = APP_IO_NOPULL;
    io_init.mode = APP_IO_MODE_OUTPUT;
    io_init.pin  = APP_IO_PIN_11;
    io_init.mux  = APP_IO_MUX;
    ret = app_io_init(APP_IO_TYPE_GPIOA, &io_init);
    if (ret != APP_DRV_SUCCESS)
    {
        printf("APP_GPIO_PIN0 init failed.\r\n");
    }	
		app_io_write_pin(APP_IO_TYPE_GPIOA,APP_IO_PIN_11,APP_IO_PIN_RESET);
	
    hctp_delay_ms(20);
		app_io_write_pin(APP_IO_TYPE_GPIOA,APP_IO_PIN_11,APP_IO_PIN_SET);
    //am_hal_gpio_output_set(CTP_RESET_PIN);
    hctp_delay_ms(10);
}

/**
 * @brief   cst8xx ms延时函数
 *
 * @param   毫秒
 *
 */
void hctp_delay_ms(kal_uint16 i)
{
	delay_ms(i);
    //am_util_delay_ms(i);
}

/**
 * @brief   cst8xx iic配置函数
 *
 * @param   slave_addr IIC 7bit地址
 * @param   iic_speed_k IIC 通信速率
 *
 * @return
 */
void hctp_i2c_init(kal_uint8 slave_addr, kal_uint16 iic_speed_k)
{
  //  TpIicSetSlaveAddr(slave_addr);
	app_i2c_init(&tp_params, NULL);
	//tp_pin_init();
	
}

/**
 * @brief   cst8xx iic写接口
 *
 * @param   reg 寄存器地址
 * @param   data 写入数据
 * @param   len 数据长度
 * @param   regLen 寄存器地址长度
 *
 * @return  CTP_TRUE成功
 */
kal_bool hctp_write_bytes(kal_uint16 reg, kal_uint8 *data, kal_uint16 len, kal_uint8 regLen)
{
//    uint32_t u32Status = AM_HAL_STATUS_SUCCESS;

//    uint8_t * tx_data = (uint8_t *)pvPortMalloc(len + regLen);

//    if (regLen == sizeof(kal_uint8))
//    {
//        tx_data[0] = (uint8_t)reg;
//        for (int i = 0; i < len; i++)
//            tx_data[1 + i] = data[i];
//    }
//    else
//    {
//        tx_data[0] = reg >> 8;      //寄存器高位
//        tx_data[1] = reg & 0xff;    //寄存器低位
//        for (int i = 0; i < len; i++)
//            tx_data[2 + i] = data[i];
//    }

//    u32Status = _inTpIomIicTxBlocking((uint8_t *)tx_data, len + regLen);

//    vPortFree(tx_data);

//    return (u32Status == AM_HAL_STATUS_SUCCESS) ? CTP_TRUE : CTP_FALSE;
}

/**
 * @brief   cst8xx iic读接口
 *
 * @param   reg 寄存器地址
 * @param   data 读出数据地址
 * @param   len 数据长度
 * @param   regLen 寄存器地址长度
 *
 * @return  CTP_TRUE成功
 */
uint16_t hctp_read_bytes(kal_uint16 reg, kal_uint8 *value, kal_uint16 len, kal_uint8 regLen)
{
    uint16_t ret = 0;
    if (regLen == sizeof(kal_uint8))
    {
			 ret = app_i2c_mem_read_sync(APP_I2C_ID_3, CST0XX_I2C_SLAVEADDR, reg, I2C_MEMADD_SIZE_8BIT, value, len, 1000);
       // u32Status = _inTpIomIicRxBlocking8RegAddr(reg, value, len);
    }
//    else
//    {
//        uint16_t _reg16 = ((reg >> 8) & 0xFF) | ((reg << 8) & 0xFF00);
//        u32Status = _inTpIomIicRxBlocking(_reg16, value, len);
//    }
    return (ret == 1) ? 1 : 0;
}
